package com.zllkj.mm.api.network;


import com.ding.library.CaptureInfoInterceptor;
import com.zllkj.mm.BuildConfig;
import com.zllkj.mm.api.network.converter.JsonConverterFactory;
import com.zllkj.mm.api.remote.AccountApi;
import com.zllkj.mm.api.remote.AddressApi;
import com.zllkj.mm.api.remote.Api;
import com.zllkj.mm.api.remote.CommonApi;
import com.zllkj.mm.api.remote.DynamicApi;
import com.zllkj.mm.api.remote.FreeTimeApi;
import com.zllkj.mm.api.remote.GoodsApi;
import com.zllkj.mm.api.remote.LiveApi;
import com.zllkj.mm.api.remote.OrderApi;
import com.zllkj.mm.api.remote.RelationApi;
import com.zllkj.mm.api.remote.ShopApi;
import com.zllkj.mm.api.remote.ShoppingCartApi;
import com.zllkj.mm.api.remote.UserApi;
import com.zllkj.mm.api.remote.WXApi;
import com.zllkj.mm.common.ClientConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.Address;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

public class RetrofitHelper {

    private static RetrofitHelper instance;

    private Api mApi;

    private AccountApi mAccountApi;

    private WXApi mWXApi;

    private UserApi mUserApi;

    private LiveApi mLiveApi;

    private CommonApi mCommonApi;

    private RelationApi mRelationApi;

    private Retrofit retrofit;

    private OrderApi orderApi;

    private GoodsApi goodsApi;

    private ShoppingCartApi shoppingCartApi;

    private ShopApi shopApi;

    private DynamicApi dynamicApi;

    private AddressApi addressApi;

    private FreeTimeApi freeTimeApi;

    public static RetrofitHelper getInstance() {
        if (instance == null) {
            synchronized (RetrofitHelper.class) {
                instance = new RetrofitHelper();
            }
        }
        return instance;
    }

    public FreeTimeApi getFreeTimeApi() {
        if (freeTimeApi == null) {
            freeTimeApi = configRetrofit(FreeTimeApi.class, true);
        }
        return freeTimeApi;
    }


    private RetrofitHelper() {
        retrofit = new Retrofit.Builder()
                .baseUrl(ClientConfig.BASE_URL)
                .client(configClient(true))
                .addConverterFactory(JsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }


    public Api getApi() {
        if (mApi == null) {
            mApi = configRetrofit(Api.class, true);
        }
        return mApi;
    }

    public DynamicApi getDynamicApi(){
        if(dynamicApi == null) {
            dynamicApi = configRetrofit(DynamicApi.class, true);
        }
        return dynamicApi;
    }

    public ShopApi getShopApi() {
        if (shopApi == null) {
            shopApi = configRetrofit(ShopApi.class, true);
        }
        return shopApi;
    }

    public OrderApi getOrderApi() {
        if (orderApi == null) {
            orderApi = configRetrofit(OrderApi.class, true);
        }
        return orderApi;
    }

    public GoodsApi getGoodsApi() {
        if (goodsApi == null) {
            goodsApi = configRetrofit(GoodsApi.class, true);
        }
        return goodsApi;
    }

    public AddressApi getAddressApi() {
        if (addressApi == null) {
            addressApi = configRetrofit(AddressApi.class, true);
        }
        return addressApi;
    }

    public AccountApi getAccountApi() {
        if (mAccountApi == null) {
            mAccountApi = configRetrofit(AccountApi.class, false);
        }
        return mAccountApi;
    }

    public UserApi getUserApi() {
        if (mUserApi == null) {
            mUserApi = configRetrofit(UserApi.class, true);
        }

        return mUserApi;
    }

    public ShoppingCartApi getShoppingCartApi(){
        if(shoppingCartApi == null){
            shoppingCartApi = configRetrofit(ShoppingCartApi.class, true);
        }
        return shoppingCartApi;
    }

    public LiveApi getLiveApi() {
        if (mLiveApi == null) {
            mLiveApi = configRetrofit(LiveApi.class, true);
        }
        return mLiveApi;
    }

    public CommonApi getCommonApi() {
        if (mCommonApi == null) {
            mCommonApi = configRetrofit(CommonApi.class, true);
        }

        return mCommonApi;
    }

    public RelationApi getRelationApi() {
        if (mRelationApi == null) {
            mRelationApi = configRetrofit(RelationApi.class, true);
        }

        return mRelationApi;
    }

    public WXApi getWXApi() {
        if (mWXApi == null) {
            mWXApi = configRetrofit(WXApi.class, WXApi.WX_HOST, false);
        }
        return mWXApi;
    }


    private OkHttpClient configClient(final  boolean haveToken) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (BuildConfig.DEBUG) {
            //app内抓包工具
           builder.addInterceptor(new CaptureInfoInterceptor());
        }
//        File cacheFile = new File(MDConstant.PATH_CACHE);
//        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
        // 设置Token
        if (haveToken) {
            builder.addInterceptor(new SessionInterceptor());
        }
        // 设置超时
        builder.connectTimeout(100, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        // 错误重连
        builder.retryOnConnectionFailure(true);

        return builder.build();
    }

    public Retrofit  getUploadFileRetrofitClient(){
        return new Retrofit.Builder()
                .baseUrl(ClientConfig.BASE_URL)
                .addConverterFactory(JsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }


    private <T> T configRetrofit(Class<T> service) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ClientConfig.BASE_URL)
                .client(configClient(true))
                .addConverterFactory(JsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        return retrofit.create(service);
    }

    private <T> T configRetrofit(Class<T> service, boolean haveToken) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ClientConfig.BASE_URL)
                .client(configClient(haveToken))
                .addConverterFactory(JsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        return retrofit.create(service);
    }


    private <T> T configRetrofit(Class<T> service, String url, boolean haveToken) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(configClient(haveToken))
                .addConverterFactory(JsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        return retrofit.create(service);
    }







    public <T> T create(Class<T> service) {
        return retrofit.create(service);
    }


}
